package top.as.sean.api.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.as.sean.biz.system.IDataDict;
import top.as.sean.common.response.Result;
import top.as.sean.dao.dto.system.DeleteDto;
import top.as.sean.dao.dto.system.DictTypeEdit;
import top.as.sean.dao.dto.system.DictTypePage;
import top.as.sean.dao.dto.system.DictTypeAdd;
import top.as.sean.dao.dto.system.DictValueAdd;
import top.as.sean.dao.dto.system.DictValueEdit;
import top.as.sean.dao.dto.system.DictValuePage;
import top.as.sean.dao.entity.pojo.system.DataDict;

import java.util.Map;

/**
 * 数据字典
 *
 * @author: ArchieSean
 * @create: 2022/11/6
 */
@RestController
@RequestMapping("dict")
@RequiredArgsConstructor
public class DataDictController {

    private final IDataDict iDataDict;

    /**
     * 根据字典编码查询对应字典值
     *
     * @param dictCode 字典编码
     * @return map
     */
    @GetMapping("{dictCode}")
    public Result<Map<String, String>> getDataValue(@PathVariable(name = "dictCode") String dictCode) {
        return Result.success(iDataDict.getDictData(dictCode));
    }

    /**
     * 字典类型分页
     *
     * @param dto 入参
     * @return page
     */
    @GetMapping("type/page")
    public Result<Page<DataDict>> getTypePage(@Validated @ModelAttribute DictTypePage dto) {
        return Result.success(iDataDict.getTypePage(dto));
    }

    /**
     * 新增数据字典类型
     *
     * @return boolean
     */
    @PostMapping("type/add")
    public Result<Boolean> addType(@Validated @RequestBody DictTypeAdd dto) {
        return Result.success(iDataDict.addType(dto));
    }


    /**
     * 删除字典及其值
     *
     * @param dto ids
     * @return boolean
     */
    @PostMapping("type/del")
    public Result<Boolean> removeBatch(@Validated @RequestBody DeleteDto dto) {
        return Result.success(iDataDict.removeBatchTypes(dto.getIds()));
    }

    /**
     * 编辑数据字典类型
     *
     * @return boolean
     */
    @PutMapping("type/edit")
    public Result<Boolean> editType(@Validated @RequestBody DictTypeEdit dto) {
        return Result.success(iDataDict.editType(dto));
    }

    /**
     * 新增数据字典值
     *
     * @param dto 入参
     * @return boolean
     */
    @PostMapping("value/add")
    public Result<Boolean> addValue(@Validated @RequestBody DictValueAdd dto) {
        return Result.success(iDataDict.addValue(dto));
    }

    /**
     * 字典值分页
     *
     * @param dto 入参
     * @return page
     */
    @GetMapping("value/page")
    public Result<Page<DataDict>> getValuePage(@Validated @ModelAttribute DictValuePage dto) {
        return Result.success(iDataDict.getValuePage(dto));
    }

    /**
     * 编辑数据字典值
     *
     * @param dto 入参
     * @return boolean
     */
    @PutMapping("value/edit")
    public Result<Boolean> editValue(@Validated @RequestBody DictValueEdit dto) {
        return Result.success(iDataDict.editValue(dto));
    }

    /**
     * 删除数据字典值
     *
     * @param id id
     * @return boolean
     */
    @DeleteMapping("value/delete")
    public Result<Boolean> delValue(@RequestParam("id") Long id) {
        return Result.success(iDataDict.delValue(id));
    }
}
